// src/assets/js/HomeView.js
import '@/assets/css/HomeView.css'

import { onMounted, ref } from 'vue'
import { UserFilled } from '@element-plus/icons-vue'
import { Document, Menu as IconMenu, Location, Setting } from '@element-plus/icons-vue'
import { getCookie } from '@/utils/cookie'

export default {
  name: 'HomeView',
  components: {
    Document,
    IconMenu,
    Location,
    Setting,
    UserFilled,
  },
  setup() {
    const isCollapse = ref(true)
    const activeIndex = ref('0')
    const user = ref(null)

    const handleOpen = (key, keyPath) => {
      console.log(key, keyPath)
    }

    const handleClose = (key, keyPath) => {
      console.log(key, keyPath)
    }

    // 添加这个新方法
    const toggleCollapse = () => {
      isCollapse.value = !isCollapse.value
    }

    onMounted(() => {
      document.title = 'Home'

      const userCookie = getCookie('user')
      if (userCookie) {
        try {
          user.value = JSON.parse(userCookie)
          console.log('用户信息:', user.value)
        } catch (error) {
          console.error('解析用户信息失败:', error)
          user.value = null
        }
      }
    })

    return {
      user,
      isCollapse,
      activeIndex,
      handleOpen,
      handleClose,
      toggleCollapse, // 返回新方法
    }
  },
  methods: {
    handleSelect(index) {
      this.activeIndex = index
      switch (index) {
        case '1':
          // console.log(this.activeIndex)
          this.$router.push('/products')
          break
        case '2':
          this.$router.push('/dependencies')
          break
        case '3':
          this.$router.push('/testReport')
          break
        case '4':
          this.$router.push('/codeDiff')
          break
        case '5':
          this.$router.push('/config')
          break

        default:
          this.$router.push('/home/main')
      }
    },
    reload() {
      // console.log(this.activeIndex)
      this.activeIndex = '0'
      this.$router.push('/main')
    },
  },
}
